﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PocketDOHS
{
    public partial class CondDent : Form
    {
        private TextBox txtFocus;

        public CondDent()
        {
            InitializeComponent();
        }

        private void btnAvanc_Click(object sender, EventArgs e)
        {
            FormsMethods.nextForm();
        }

        private void btnVoltar_Click(object sender, EventArgs e)
        {
            FormsMethods.prevForm();
        }

        private void btnLimp_Click(object sender, EventArgs e)
        {
            Limpar_txt_Form();
            txtCoroa18.Focus();
        }

        private void txtCoroaFocus(object sender, EventArgs e)
        {
            btn0.Enabled = true;
            btn1.Enabled = true;
            btn2.Enabled = true;
            btn3.Enabled = true;
            btn4.Enabled = true;
            btn5.Enabled = true;
            btn6.Enabled = true;
            btn7.Enabled = true;
            btn8.Enabled = true;
            btn9.Enabled = true;
            btnT.Enabled = true;
            btnA.Enabled = true;
            btnB.Enabled = true;
            btnC.Enabled = true;
            btnD.Enabled = true;
            btnE.Enabled = true;
            btnF.Enabled = true;
            btnG.Enabled = true;
            btnT.Enabled = true;
            btnP.Enabled = false;
            txtFocus = (TextBox)sender;
        }

        private void BotoesFalso()
        {
            btn0.Enabled = false;
            btn1.Enabled = false;
            btn2.Enabled = false;
            btn3.Enabled = false;
            btn4.Enabled = false;
            btn5.Enabled = false;
            btn6.Enabled = false;
            btn7.Enabled = false;
            btn8.Enabled = false;
            btn9.Enabled = false;
            btnA.Enabled = false;
            btnB.Enabled = false;
            btnC.Enabled = false;
            btnD.Enabled = false;
            btnE.Enabled = false;
            btnF.Enabled = false;
            btnG.Enabled = false;
            btnP.Enabled = false;
            btnT.Enabled = false;
        }

        private void txtRaizFocus(object sender, EventArgs e)
        {
            BotoesFalso();
            txtFocus = (TextBox)sender;
            ValidacaoCodRaiz((TextBox)sender);
        }

        private void txtTratamentoFocus(object sender, EventArgs e)
        {
            BotoesFalso();
            txtFocus = (TextBox)sender;
            ValidacaoCodTratamento((TextBox)sender);
        }

        private void WriteTextBox(object sender, EventArgs e)
        {
            FormsMethods.WriteTextBox((Button)sender, txtFocus, this);
        }

        private void ValidacaoCodRaiz(TextBox sender)
        {
            string codCoroa = "";

            switch (sender.Name)
            {
                case "txtRaiz18":
                    codCoroa = txtCoroa18.Text;
                    break;
                case "txtRaiz17":
                    codCoroa = txtCoroa17.Text;
                    break;
                case "txtRaiz16":
                    codCoroa = txtCoroa16.Text;
                    break;
                case "txtRaiz15":
                    codCoroa = txtCoroa15.Text;
                    break;
                case "txtRaiz14":
                    codCoroa = txtCoroa14.Text;
                    break;
                case "txtRaiz13":
                    codCoroa = txtCoroa13.Text;
                    break;
                case "txtRaiz12":
                    codCoroa = txtCoroa12.Text;
                    break;
                case "txtRaiz11":
                    codCoroa = txtCoroa11.Text;
                    break;
            }
            if (codCoroa == "4" || codCoroa == "5" || codCoroa == "8" || codCoroa == "E")
            {
                btn8.Enabled = true;
                btn9.Enabled = true;
            }
            else
            {
                btn0.Enabled = true;
                btn1.Enabled = true;
                btn2.Enabled = true;
                btn3.Enabled = true;
                btn7.Enabled = true;
                btn8.Enabled = true;
                btn9.Enabled = true;
            }
        }

        private void ValidacaoCodTratamento(TextBox sender)
        {
            string codCoroa = "";
            string codRaiz = "";

            switch (sender.Name)
            {
                case "txtTrat18":
                    codCoroa = txtCoroa18.Text;
                    codRaiz = txtRaiz18.Text;
                    break;
                case "txtTrat17":
                    codCoroa = txtCoroa17.Text;
                    codRaiz = txtRaiz17.Text;
                    break;
                case "txtTrat16":
                    codCoroa = txtCoroa16.Text;
                    codRaiz = txtRaiz16.Text;
                    break;
                case "txtTrat15":
                    codCoroa = txtCoroa15.Text;
                    codRaiz = txtRaiz15.Text;
                    break;
                case "txtTrat14":
                    codCoroa = txtCoroa14.Text;
                    codRaiz = txtRaiz14.Text;
                    break;
                case "txtTrat13":
                    codCoroa = txtCoroa13.Text;
                    codRaiz = txtRaiz13.Text;
                    break;
                case "txtTrat12":
                    codCoroa = txtCoroa12.Text;
                    codRaiz = txtRaiz12.Text;
                    break;
                case "txtTrat11":
                    codCoroa = txtCoroa11.Text;
                    codRaiz = txtRaiz11.Text;
                    break;
            }

            if (codCoroa == "1" || codCoroa == "2" || codCoroa == "B" || codCoroa == "C")
            {
                btn0.Enabled = true;
                btn1.Enabled = true;
                btn2.Enabled = true;
                btn5.Enabled = true;
                btn6.Enabled = true;
                btn7.Enabled = true;
                btn8.Enabled = true;
                btn9.Enabled = true;
            }
            else if (codCoroa == "3" || codCoroa == "D")
            {
                btn0.Enabled = true;
                btn9.Enabled = true;
                if (codRaiz == "0" || codRaiz == "8")
                {
                    btn7.Enabled = true;
                    btn8.Enabled = true;
                    btnP.Enabled = true;
                }
                else if (codRaiz == "1" || codRaiz == "2")
                {
                    btn1.Enabled = true;
                    btn2.Enabled = true;
                    btn5.Enabled = true;
                    btn6.Enabled = true;
                    btn7.Enabled = true;
                    btn8.Enabled = true;
                }
                else if (codRaiz == "3")
                {
                    btn1.Enabled = true;
                    btn2.Enabled = true;
                    btn5.Enabled = true;
                    btn6.Enabled = true;
                    btn7.Enabled = true;
                    btn8.Enabled = true;
                    btnP.Enabled = true;
                }
                else if (codRaiz == "7")
                {
                    btn6.Enabled = true;
                    btn7.Enabled = true;
                    btn8.Enabled = true;
                    btnP.Enabled = true;
                }
                else
                    btnP.Enabled = true;
            }
            else if (codCoroa == "4" || codCoroa == "5" || codCoroa == "8" || codCoroa == "E")
            {
                btn7.Enabled = true;
                btn8.Enabled = true;
                btn9.Enabled = true;
            }
            else if (codCoroa == "6" || codCoroa == "F")
            {
                btn0.Enabled = true;
                btn9.Enabled = true;
                if (codRaiz == "0")
                {
                    btn7.Enabled = true;
                    btn8.Enabled = true;
                    btnP.Enabled = true;
                }
                else if (codRaiz == "1" || codRaiz == "2")
                {
                    btn1.Enabled = true;
                    btn2.Enabled = true;
                    btn5.Enabled = true;
                    btn6.Enabled = true;
                    btn7.Enabled = true;
                    btn8.Enabled = true;
                }
                else if (codRaiz == "3")
                {
                    btn1.Enabled = true;
                    btn2.Enabled = true;
                    btn5.Enabled = true;
                    btn6.Enabled = true;
                    btn7.Enabled = true;
                    btn8.Enabled = true;
                    btnP.Enabled = true;
                }
                else if (codRaiz == "7")
                {
                    btn0.Enabled = true;
                    btn1.Enabled = true;
                    btn2.Enabled = true;
                    btn3.Enabled = true;
                    btn4.Enabled = true;
                    btn5.Enabled = true;
                    btn6.Enabled = true;
                    btn7.Enabled = true;
                    btn8.Enabled = true;
                    btn9.Enabled = true;
                    btnA.Enabled = true;
                    btnB.Enabled = true;
                    btnC.Enabled = true;
                    btnD.Enabled = true;
                    btnE.Enabled = true;
                    btnF.Enabled = true;
                    btnP.Enabled = true;
                }
            }
            else if (codCoroa == "7" || codCoroa == "G")
            {
                btn0.Enabled = true;
                btn1.Enabled = true;
                btn2.Enabled = true;
                btn3.Enabled = true;
                btn4.Enabled = true;
                btn5.Enabled = true;
                btn6.Enabled = true;
                btn7.Enabled = true;
                btn8.Enabled = true;
                btn9.Enabled = true;
                btnA.Enabled = true;
                btnB.Enabled = true;
                btnC.Enabled = true;
                btnD.Enabled = true;
                btnE.Enabled = true;
                btnF.Enabled = true;
                btnP.Enabled = true;
            }
        }

        #region CoroaChanged

        private void txtCoroa18_TextChanged(object sender, EventArgs e)
        {
            txtRaiz18.Text = "0";
            txtTrat18.Text = "0";
            txtRaiz18.Focus();
        }

        private void txtCoroa17_TextChanged(object sender, EventArgs e)
        {
            txtRaiz17.Text = "0";
            txtTrat17.Text = "0";
            txtRaiz17.Focus();
        }

        private void txtCoroa16_TextChanged(object sender, EventArgs e)
        {
            txtRaiz16.Text = "0";
            txtTrat16.Text = "0";
            txtRaiz16.Focus();
        }

        private void txtCoroa15_TextChanged(object sender, EventArgs e)
        {
            if (((txtCoroa15.Text == "0") || (txtCoroa15.Text == "A")) && ((txtRaiz15.Text == "0") || (txtRaiz15.Text == "8")))
            {
                txtTrat15.Text = "0";
                txtCoroa14.Focus();
            }
            else
                txtRaiz15.Focus();
        }

        private void txtCoroa14_TextChanged(object sender, EventArgs e)
        {
            if (((txtCoroa14.Text == "0") || (txtCoroa14.Text == "A")) && ((txtRaiz14.Text == "0") || (txtRaiz14.Text == "8")))
            {
                txtTrat14.Text = "0";
                txtCoroa13.Focus();
            }
            else
                txtRaiz14.Focus();
        }

        private void txtCoroa13_TextChanged(object sender, EventArgs e)
        {
            if (((txtCoroa13.Text == "0") || (txtCoroa13.Text == "A")) && ((txtRaiz13.Text == "0") || (txtRaiz13.Text == "8")))
            {
                txtTrat13.Text = "0";
                txtCoroa12.Focus();
            }
            else
                txtRaiz13.Focus();
        }

        private void txtCoroa12_TextChanged(object sender, EventArgs e)
        {
            if (((txtCoroa12.Text == "0") || (txtCoroa12.Text == "A")) && ((txtRaiz12.Text == "0") || (txtRaiz12.Text == "8")))
            {
                txtTrat12.Text = "0";
                txtCoroa11.Focus();
            }
            else
                txtRaiz12.Focus();
        }

        private void txtCoroa11_TextChanged(object sender, EventArgs e)
        {
            if (((txtCoroa11.Text == "0") || (txtCoroa11.Text == "A")) && ((txtRaiz11.Text == "0") || (txtRaiz11.Text == "8")))
            {
                txtTrat11.Text = "0";
                btnAvanc.Focus();
            }
            else
                txtRaiz11.Focus();
        }

        #endregion

        #region RaizChanged

        private void txtRaiz18_TextChanged(object sender, EventArgs e)
        {
            txtTrat18.Text = "0";
            txtTrat18.Focus();
        }

        private void txtRaiz17_TextChanged(object sender, EventArgs e)
        {
            txtTrat17.Text = "0";
            txtTrat17.Focus();
        }

        private void txtRaiz16_TextChanged(object sender, EventArgs e)
        {
            txtTrat16.Text = "0";
            txtTrat16.Focus();
        }

        private void txtRaiz15_TextChanged(object sender, EventArgs e)
        {
            txtTrat15.Text = "0";
            txtTrat15.Focus();
        }

        private void txtRaiz14_TextChanged(object sender, EventArgs e)
        {
            txtTrat14.Text = "0";
            txtTrat14.Focus();
        }

        private void txtRaiz13_TextChanged(object sender, EventArgs e)
        {
            txtTrat13.Text = "0";
            txtTrat13.Focus();
        }

        private void txtRaiz12_TextChanged(object sender, EventArgs e)
        {
            txtTrat12.Text = "0";
            txtTrat12.Focus();
        }

        private void txtRaiz11_TextChanged(object sender, EventArgs e)
        {
            txtTrat11.Text = "0";
            txtTrat11.Focus();
        }

        #endregion

        #region TratamentoChanged

        private void txtTrat18_TextChanged(object sender, EventArgs e)
        {
            txtCoroa17.Focus();
        }

        private void txtTrat17_TextChanged(object sender, EventArgs e)
        {
            txtCoroa16.Focus();
        }

        private void txtTrat16_TextChanged(object sender, EventArgs e)
        {
            txtCoroa15.Focus();
        }

        private void txtTrat15_TextChanged(object sender, EventArgs e)
        {
            txtCoroa14.Focus();
        }

        private void txtTrat14_TextChanged(object sender, EventArgs e)
        {
            txtCoroa13.Focus();
        }

        private void txtTrat13_TextChanged(object sender, EventArgs e)
        {
            txtCoroa12.Focus();
        }

        private void txtTrat12_TextChanged(object sender, EventArgs e)
        {
            txtCoroa11.Focus();
        }

        private void txtTrat11_TextChanged(object sender, EventArgs e)
        {
            btnAvanc.Focus();
        }

        #endregion

        private void CondDent_Activated(object sender, EventArgs e)
        {
            if (FormsMethods.Inicializa[FormsMethods.VetIndex])
            {
                Limpar_txt_Form();
                FormsMethods.Inicializa[FormsMethods.VetIndex] = false;
                txtCoroa18.Focus();
            }
            if (FormsMethods.Altera.ToArray()[FormsMethods.VetIndex])
            {
                FormsMethods.preencheForms();
                FormsMethods.Altera[FormsMethods.VetIndex] = false;
            }
        }

        private void Limpar_txt_Form()
        {
            txtCoroa11.Text = "0";
            txtCoroa12.Text = "0";
            txtCoroa13.Text = "0";
            txtCoroa14.Text = "0";
            txtCoroa15.Text = "0";
            txtCoroa16.Text = "0";
            txtCoroa17.Text = "0";
            txtCoroa18.Text = "0";
            txtRaiz11.Text = "0";
            txtRaiz12.Text = "0";
            txtRaiz13.Text = "0";
            txtRaiz14.Text = "0";
            txtRaiz15.Text = "0";
            txtRaiz16.Text = "0";
            txtRaiz17.Text = "0";
            txtRaiz18.Text = "0";
            txtTrat11.Text = "0";
            txtTrat12.Text = "0";
            txtTrat13.Text = "0";
            txtTrat14.Text = "0";
            txtTrat15.Text = "0";
            txtTrat16.Text = "0";
            txtTrat17.Text = "0";
            txtTrat18.Text = "0";
        }

    }
}